package com.pri.chat.activity;

import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.netease.nim.uikit.net.SharedHelper;
import com.netease.nim.uikit.net.entity.BaseBean;
import com.netease.nim.uikit.net.entity.LookMemberListBean;
import com.netease.nim.uikit.net.entity.LookMemberResult;
import com.netease.nim.uikit.net.rxjava.HttpMethods;
import com.netease.nim.uikit.net.subscribers.ProgressSubscriber;
import com.netease.nim.uikit.net.subscribers.SubscriberOnNextListener;
import com.pri.chat.R;
import com.pri.chat.adapter.LookMemberAdapter;
import com.pri.chat.utils.Des3Util;
import com.pri.chat.utils.bars.StatusBarUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.vondear.rxtool.RxActivityTool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 描述：添加类的描述
 *
 * @author
 * @time 2020/5/9
 */
public class LookMemberActivity extends AppCompatActivity implements BaseQuickAdapter.OnItemChildClickListener, BaseQuickAdapter.OnItemClickListener {
    @BindView(R.id.rec_main)
    RecyclerView recMain;
    @BindView(R.id.none)
    ImageView none;
    @BindView(R.id.srl_all)
    SmartRefreshLayout srlAll;
    @BindView(R.id.iv_bg)
    View iv_bg;
    @BindView(R.id.iv_back)
    View iv_back;
    @BindView(R.id.headPic)
    ImageView headPic;
    @BindView(R.id.tv_hi)
    TextView tv_hi;
    @BindView(R.id.lookNumber)
    TextView lookNumber;
    @BindView(R.id.btn_kthy)
    TextView btn_kthy;
    private RelativeLayout view;




    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_look_member_layout);
        ButterKnife.bind(this);
        view = findViewById(R.id.rl_center);
        boolean isVip = getIntent().getExtras().getBoolean("isVIp");
//        if (isVip) {
            iv_bg.setVisibility(View.GONE);
            view.setBackgroundColor(getResources().getColor(R.color.white));
            StatusBarUtil.setStatusBarMode(this,true,R.color.white);
//        } else {
//            view.setBackgroundColor(getResources().getColor(R.color.look_bg));
//            StatusBarUtil.setStatusBarMode(this,true,R.color.look_bg);
//        }


        srlAll.setEnableLoadmore(false);
        initLayout();
        initSrl();
        RequestOptions options = new RequestOptions()
                .placeholder(R.mipmap.img_default)//图片加载出来前，显示的图片
                .fallback( R.mipmap.img_default) //url为空的时候,显示的图片
                .error(R.mipmap.img_default)
                .transform(new CenterCrop(),new CircleCrop());
        Glide.with(this).load(SharedHelper.readHeadPic(this)).apply(options).into(headPic);
        tv_hi.setText("HI，"+SharedHelper.readNick(this));
    }

    @Override
    protected void onResume() {
        super.onResume();
        refresh();
    }

    private List<LookMemberListBean> mList;
    private LookMemberAdapter mAdapter;
    private int page = 1;
    private boolean isShowDialog = false;

    private void initLayout() {
        mList = new ArrayList<>();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recMain.setLayoutManager(linearLayoutManager);
        mAdapter = new LookMemberAdapter(R.layout.item_look_member, mList);
        recMain.setAdapter(mAdapter);
        mAdapter.setOnItemChildClickListener(this);
        mAdapter.setOnItemClickListener(this);
    }

    private void initData() {
        SubscriberOnNextListener<BaseBean<LookMemberResult>> onNextListener = baseBean -> {
            if (baseBean.getData().getList().isEmpty() && page == 1) {
                mAdapter.notifyDataSetChanged();
            } else {
                mList.addAll(baseBean.getData().getList());
                mAdapter.notifyDataSetChanged();
            }
            if(baseBean.getData().getNumber() == 0){
                iv_bg.setVisibility(View.GONE);
                view.setBackgroundColor(getResources().getColor(R.color.white));
                StatusBarUtil.setStatusBarMode(this,true,R.color.white);
            }else{
                view.setBackgroundColor(getResources().getColor(R.color.look_bg));
                StatusBarUtil.setStatusBarMode(this,true,R.color.look_bg);
                iv_bg.setVisibility(View.VISIBLE);
                lookNumber.setText(Html.fromHtml("最近有<font color='red'>"+baseBean.getData().getNumber()+"</font>人来看过你"));
            }
        };
        Map<String,Object> dtModel = new HashMap<>();//登录上传参数
        dtModel.put("memberId",SharedHelper.readId(this));
        dtModel.put("pageIndex",page);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(dtModel));
        HttpMethods.getInstance().getLookMeMember(new ProgressSubscriber<>(onNextListener, this, isShowDialog), requestBody);
    }

    private void initSrl() {
        srlAll.setOnRefreshListener(refresh_layout -> {
            refresh_layout.finishRefresh(1000);
            refresh();
        });
        srlAll.setOnLoadmoreListener(refresh_layout -> {
            refresh_layout.finishLoadmore(1000);
            isShowDialog = false;
            page++;
            initData();
        });
    }

    @OnClick({R.id.iv_back,R.id.headPic,R.id.btn_kthy})
    public void onClick(View v){
        if(v.getId() == R.id.iv_back){
            finish();
        }else if(v.getId() == R.id.headPic){
            RxActivityTool.skipActivity(this,PeopleMsgActivity.class);
        }else if(v.getId() == R.id.btn_kthy){
            RxActivityTool.skipActivity(this,VipCenterActivity.class);
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Bundle bundle =new Bundle();
        bundle.putString("id",mList.get(position).getId());
        RxActivityTool.skipActivity(this, OtherPeopleMsgActivity.class,bundle);
    }

    private void refresh() {
        page = 1;
        isShowDialog = false;
        mList.clear();
        mAdapter.notifyDataSetChanged();
        initData();
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        Bundle bundle =new Bundle();
        bundle.putString("id",mList.get(position).getId());
        RxActivityTool.skipActivity(this, OtherPeopleMsgActivity.class,bundle);
    }
}
